package com.AX.mapper;

import com.AX.entity.GeoPoint;
import com.AX.entity.NavNode;
import org.apache.ibatis.annotations.*;

import java.util.List;

//查询停车场节点
public interface NavNodeMapper {
    @Select("""
        SELECT 
            node_id, 
            parking_lot_id, 
            node_type, 
            is_accessible,
            ST_AsText(position) AS position 
        FROM nav_nodes 
        WHERE parking_lot_id = #{lotId}
    """)
    @Results({
            @Result(property = "nodeId", column = "node_id"),
            @Result(property = "parkingLotId", column = "parking_lot_id"),
            @Result(property = "nodeType", column = "node_type"),
            @Result(property = "accessible", column = "is_accessible"),
            @Result(property = "position", column="position" ,typeHandler=com.AX.handler.GeoPointTypeHandler.class)
    })
    List<NavNode> selectByParkingLot(@Param("lotId") Integer lotId);

    @Select("""
        SELECT 
            n.node_id, 
            n.parking_lot_id, 
            n.node_type, 
            n.is_accessible,
            ST_AsText(n.position) AS position 
        FROM nav_nodes n 
        ORDER BY ST_Distance(n.position, ST_SRID(Point(#{lng}, #{lat}), 4326)) 
        LIMIT 1
    """)
    @Results({
            @Result(property = "nodeId", column = "node_id"),
            @Result(property = "parkingLotId", column = "parking_lot_id"),
            @Result(property = "nodeType", column = "node_type"),
            @Result(property = "accessible", column = "is_accessible"),
            @Result(property = "position", column="position" ,typeHandler=com.AX.handler.GeoPointTypeHandler.class)
    })
    NavNode findNearestNode(@Param("lng") Double lng,
                            @Param("lat") Double lat,
                            @Param("lotId") Integer lotId);

    @Select("""
        SELECT 
            n.node_id, 
            n.parking_lot_id, 
            n.node_type, 
            n.is_accessible,
            ST_AsText(n.position) AS position 
        FROM nav_nodes n 
        JOIN parking_spaces s ON s.nearest_node = n.node_id 
        WHERE s.space_id = #{spaceId}
    """)
    @Results({
            @Result(property = "nodeId", column = "node_id"),
            @Result(property = "parkingLotId", column = "parking_lot_id"),
            @Result(property = "nodeType", column = "node_type"),
            @Result(property = "accessible", column = "is_accessible"),
            @Result(property = "position", column="position" ,typeHandler=com.AX.handler.GeoPointTypeHandler.class)
    })
    NavNode findBySpaceId(@Param("spaceId") String spaceId);
}
